package com.tz.online.book.action;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tz.online.book.service.IBookService;
import com.tz.online.cart.service.IShoppingCart;
import com.tz.online.common.PageBean;
import com.tz.online.entity.Book;
import com.tz.online.util.BeanFactory;

/**
 * Servlet implementation class BookAction
 */
@WebServlet("/book/*")
public class BookAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/** 业务接口 */
	private IBookService bookService;
	private IShoppingCart shoppingCart;

	/** 请求资源URL常量 */
	private static final String _INDEX = "/index";  //首页
	private static final String _SPEICAL = "/special"; //特价图书
	private static final String _CATEGORY = "/category"; //分类大全
	private static final String _DETAIL = "/detail"; //详细
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookAction() {
		super();
		//初始化业务接口
		this.bookService = (IBookService) BeanFactory.getBean("bookService");
		this.shoppingCart = (IShoppingCart) BeanFactory.getBean("cartService");
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.获取 servletpath
		String servletPath = request.getServletPath();
		if (servletPath.equals("/book")) {
			// 2.拿到 pathinfo
			String pathInfo = request.getPathInfo();
			// 3.根据这个pathinfo 来做出不同的处理
			switch (pathInfo) {
			case _INDEX:
				//获取精选图书
				PageBean<Book> bestBook = bookService.queryByCondition(1, 2, "精选");
				//获取新书
				PageBean<Book> newBook = bookService.queryByCondition(1, 3, "新书");
				//推荐图书 [全部由过滤器来完成】
//				PageBean<Book> tuijianBook = bookService.queryByCondition(1,3, "推荐");
				//绑定参数
				request.setAttribute("bestBook", bestBook);
				request.setAttribute("newBook", newBook);
//				request.setAttribute("tuijianBook", tuijianBook);
				//转发
				forward(request, response, true, "/WEB-INF/jsp/book/index.jsp");
				break;
			case _CATEGORY:
				// 获取请求参数
				String pageStr = request.getParameter("page");
				int page = pageStr == null ? 1 : Integer.parseInt(pageStr);
				String cate = request.getParameter("cate");
				//获取所有图书
				PageBean<Book> allBooks = bookService.queryByCondition(page, 8, cate);
				//推荐图书
//				tuijianBook = bookService.queryByCondition(1,3, "推荐");
				//绑定参数
				request.setAttribute("allBook", allBooks);
//				request.setAttribute("tuijianBook", tuijianBook);
				//转换
				forward(request, response, true, "/WEB-INF/jsp/book/category.jsp");
				break;
			case _SPEICAL:
				//获取请求参数
				pageStr = request.getParameter("page");
				page = pageStr == null ? 1 : Integer.parseInt(pageStr);
				//获取特价图书
				PageBean<Book> speicalBooks = bookService.queryByCondition(page, 4, "特价");
				//推荐图书
//				tuijianBook = bookService.queryByCondition(1,3, "推荐");
				//绑定参数
				request.setAttribute("specialBook", speicalBooks);
//				request.setAttribute("tuijianBook", tuijianBook);
				//转换
				forward(request, response, true, "/WEB-INF/jsp/book/special.jsp");
				break;
			case _DETAIL:
				//获取 请求参数
				String idStr = request.getParameter("id");
				if(idStr != null) {
					Book b = bookService.queryById(Long.parseLong(idStr));
					//绑定参数
					request.setAttribute("book", b);
					//推荐图书
//					PageBean<Book> tuijianBook2 = bookService.queryByCondition(1,3, "推荐");
//					request.setAttribute("tuijianBook", tuijianBook2);
					//跳 转
					request.getSession().setAttribute("shoppingCart", shoppingCart);
					forward(request, response, true, "/WEB-INF/jsp/book/detail.jsp");
				}
				break;
			default:
				// 非法的请求
				response.sendError(HttpServletResponse.SC_BAD_REQUEST, "你所请求的路径是非法的");
				break;
			}

		} else {
			// 不合法法的 servletpath
			// ...
		}
	}
	
	/***************
	 * 转发或重定向请求
	 * 
	 * @param request
	 * @param response
	 * @param b
	 *            为真，则转换请求，为false,则重定向请求
	 * @param target
	 *            目标资源
	 * @throws IOException
	 * @throws ServletException
	 */
	private void forward(HttpServletRequest request, HttpServletResponse response, boolean b, String target)
			throws ServletException, IOException {
		if (b) {
			RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
			rd.forward(request, response);
			return;
		} else {
			String contextPath = getServletContext().getContextPath();
			response.sendRedirect(contextPath+target);
			return ;
		}
	}

}
